package LeetCode;

import java.util.*;

/**
 * Created by kechelle on 2020/8/12 10:55
 * 字母异位词分组
 * strs = ["are","bat","ear","code","tab","era"]
 */
public class Code49 {
    public List<List<String>> groupAnagrams(String[] strs){
        if (null==strs || strs.length==0)  return new ArrayList<>();
        Map<String,List<String>> map = new HashMap<>();
        for (int i=0;i<strs.length;i++){
            char[] ch = strs[i].toCharArray();
            Arrays.sort(ch);
            String key = String.valueOf(ch);
            if (!map.containsKey(key)) map.put(key,new ArrayList<>());
            map.get(key).add(strs[i]);
        }
        return new ArrayList<>(map.values());
    }
}
